<?php


namespace app\admin\controller;


use app\common\model\UserLog;
use think\Db;
use think\Request;

class Login extends Base
{
    /**
     * 登陆
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        if (Request::instance()->isAjax()){
            $data = input();
            $userInfo = Db::name('user')->where('username',$data['username'])->find();
            if (!empty($userInfo)){
                if ($data['password'] == encry_code($userInfo['password'],'DECODE')){
                    session('adminUser',$userInfo);
                    UserLog::saveUserLog($userInfo['id'],'登陆系统');
                    $this->success('登陆成功');
                }else{
                    $this->error('密码错误');
                }
            }else{
                $this->error('用户名不存在');
            }
        }
        return view();
    }

    /**
     * 退出
     */
    public function loginOut(){
        session('adminUser',null);
        $this->redirect('login/index');
    }
}